package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class MessageSentPeer extends BasePeer{
	public static final String tableName = "MessageSent";
	public static final String MESSAGEID = "messageId";
	public static final String USERID = "userId";
	public static final String FROMUSERID = "fromUserId";
	public static final String TOUSERID = "toUserId";
	public static final String TOUSER = "toUser";
	public static final String TITLE = "title";
	public static final String CONTENT = "content";
	public static final String POSTTIME = "postTime";
	public static final String[] columnNames=new String[]{"messageId","userId","fromUserId","toUserId","toUser","title","content","postTime"};
	public static MessageSent getMessageSentByPKs(Object messageId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(MessageSentPeer.tableName,MessageSentPeer.columnNames," TRUE  AND `messageId`=? ",new Object[]{messageId,},0, 1,conn);
			MessageSent messageSent = new MessageSent();
		fill(rs,messageSent);
		return messageSent;
	}
	public static MessageSent getMessageSentReferenceColumnsOnlyByPKs(Object messageId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(MessageSentPeer.tableName,new String[]{"messageId",}," TRUE AND `messageId`=?",new Object[]{messageId,}, 0,  1,conn);
			MessageSent messageSent = new MessageSent();
		fill(rs,messageSent);
		return messageSent;
	}
	public static MessageSent getMessageSentByMessageId(long messageId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(MessageSentPeer.tableName,MessageSentPeer.columnNames,"`messageId`=?",new Object[]{messageId},0, 1,conn);
			MessageSent messageSent = new MessageSent();
		fill(rs,messageSent);
		return messageSent;
	}
	public static void fill(ResultSet rs, MessageSent messageSent) throws SQLException {
		if(rs.next()){
			try{messageSent.setMessageId(rs.getLong("messageId"));}catch(Exception ex){}
			try{messageSent.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{messageSent.setFromUserId(rs.getLong("fromUserId"));}catch(Exception ex){}
			try{messageSent.setToUserId(rs.getLong("toUserId"));}catch(Exception ex){}
			try{messageSent.setToUser(rs.getString("toUser"));}catch(Exception ex){}
			try{messageSent.setTitle(rs.getString("title"));}catch(Exception ex){}
			try{messageSent.setContent(rs.getString("content"));}catch(Exception ex){}
			try{messageSent.setPostTime(rs.getTimestamp("postTime"));}catch(Exception ex){}
			messageSent.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<MessageSent> messageSentCollection) throws SQLException {
		while(rs.next()){
			MessageSent messageSent=new MessageSent();
			try{messageSent.setMessageId(rs.getLong("messageId"));}catch(Exception ex){}
			try{messageSent.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{messageSent.setFromUserId(rs.getLong("fromUserId"));}catch(Exception ex){}
			try{messageSent.setToUserId(rs.getLong("toUserId"));}catch(Exception ex){}
			try{messageSent.setToUser(rs.getString("toUser"));}catch(Exception ex){}
			try{messageSent.setTitle(rs.getString("title"));}catch(Exception ex){}
			try{messageSent.setContent(rs.getString("content"));}catch(Exception ex){}
			try{messageSent.setPostTime(rs.getTimestamp("postTime"));}catch(Exception ex){}
			messageSent.resetModified();
			messageSentCollection.add(messageSent);
		}
	}
	public static boolean update(MessageSent messageSent, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < MessageSentPeer.columnNames.length; i++) {
			if (((MessageSentBase)messageSent).modified[i]) {
				columns.add(MessageSentPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `MessageSent` SET ");
		sql.append("WHERE TRUE AND `messageId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,messageSent.get(column));
		}
		pstmt.setObject(columns.size()+1+0,messageSent.get("messageId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(MessageSent messageSent, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (messageSent.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `MessageSent` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (messageSent.get(column) != null) {
				pstmt.setObject(i + 1 + j, messageSent.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<MessageSent> getMessageSentCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<MessageSent> collection=new Vector<MessageSent>();
		fill(rs,collection);
		return collection;
	}
}
